$(function () {

    'use strict';

    // Select2
    $('.select2').select2({minimumResultsForSearch: Infinity});

    // Select2 by showing the search
    $('.select2-show-search').select2({
        minimumResultsForSearch: ''
    });

    var content = "<option label=' + 客户级别 + '></option>";
    var levelId = $('input[name="levelId"]').val();
    $.ajax({
        type: "POST",
        url: "/crm/customer/selCusLevelList",
        success: function (data) {
            console.log(data);
            for (var i = 0; i < data.length; i++) {
                if (data[i].id == levelId) {
                    content += "<option value='" + data[i].id + "' selected='true'>" + data[i].name + "</option>";
                    continue;
                } else {
                    content += "<option value='" + data[i].id + "'>" + data[i].name + "</option>";
                }
            }
            $("#customerLevelSel").html(content);
        }
    });

    $(".id").click(function () {
        var id = $(this).attr("id");
        $.ajax({
            type: "POST",
            url: "/crm/customer/" + id,
            success: function (data) {
                var content = "<div class=\"modal-header\">\n" +
                    "<h5 class=\"modal-title tx-14 mg-b-0 tx-uppercase tx-inverse tx-bold\" id=\"modalLabel\">客户详细信息</h5>\n" +
                    "<button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-hidden=\"true\">&times;</button>\n" +
                    "</div>";

                content += "<div class=\"modal-body\">\n" +
                    "<div class=\"card pd-10 pd-sm-20\">\n" +
                    "          <h6 class=\"card-body-title\">客户信息</h6>\n" +
                    "          <div class=\"progress mg-b-20\">\n" +
                    "                <div class=\"progress-bar bg-danger ht-2 wd-100p\" role=\"progressbar\" aria-valuenow=\"100\"\n" +
                    "                     aria-valuemin=\"0\" aria-valuemax=\"100\"></div>\n" +
                    "          </div>\n" +
                    "\n" +
                    "          <dl class=\"row\">\n" +
                    "            <dt class=\"col-sm-2 tx-inverse\">客户名称</dt>\n" +
                    "            <dd class=\"col-sm-4\">\n" +
                    "              <p class=\"mg-b-10\">小红</p>\n" +
                    "            </dd>\n" +
                    "\n" +
                    "            <dt class=\"col-sm-2 tx-inverse\">性别</dt>\n" +
                    "            <dd class=\"col-sm-4\">\n" +
                    "              <p class=\"mg-b-10\">女</p>\n" +
                    "            </dd>\n" +
                    "\n" +
                    "            <dt class=\"col-sm-2 tx-inverse\">联系方式</dt>\n" +
                    "            <dd class=\"col-sm-4\">\n" +
                    "              <p class=\"mg-b-10\">13143134312</p>\n" +
                    "            </dd>\n" +
                    "\n" +
                    "            <dt class=\"col-sm-2 tx-inverse\">联系方式</dt>\n" +
                    "            <dd class=\"col-sm-4\">\n" +
                    "              <p class=\"mg-b-10\">13143134312</p>\n" +
                    "            </dd>\n" +
                    "\n" +
                    "            <dt class=\"col-sm-2 tx-inverse\">索引记录</dt>\n" +
                    "            <dd class=\"col-sm-4\">\n" +
                    "              <p class=\"mg-b-10\">A-31</p>\n" +
                    "            </dd>\n" +
                    "\n" +
                    "            <dt class=\"col-sm-2 tx-inverse\">电子邮箱</dt>\n" +
                    "            <dd class=\"col-sm-4\">\n" +
                    "              <p class=\"mg-b-10\">13143134312@11.com</p>\n" +
                    "            </dd>\n" +
                    "\n" +
                    "            <dt class=\"col-sm-2 tx-inverse\">身份证号</dt>\n" +
                    "            <dd class=\"col-sm-4\">\n" +
                    "              <p class=\"mg-b-10\">13143134312412341232</p>\n" +
                    "            </dd>\n" +
                    "\n" +
                    "            <dt class=\"col-sm-2 tx-inverse\">生日</dt>\n" +
                    "            <dd class=\"col-sm-4\">\n" +
                    "              <p class=\"mg-b-10\">13143134312</p>\n" +
                    "            </dd>\n" +
                    "\n" +
                    "            <dt class=\"col-sm-2 tx-inverse\">地址</dt>\n" +
                    "            <dd class=\"col-sm-4\">\n" +
                    "              <p class=\"mg-b-10\">13143134312</p>\n" +
                    "            </dd>\n" +
                    "\n" +
                    "            <dt class=\"col-sm-2 tx-inverse\">爱好</dt>\n" +
                    "            <dd class=\"col-sm-4\">\n" +
                    "              <p class=\"mg-b-10\">13143134312</p>\n" +
                    "            </dd>\n" +
                    "\n" +
                    "            <dt class=\"col-sm-2 tx-inverse\">需求</dt>\n" +
                    "            <dd class=\"col-sm-4\">\n" +
                    "              <p class=\"mg-b-10\">13143134312</p>\n" +
                    "            </dd>\n" +
                    "\n" +
                    "            <dt class=\"col-sm-2 tx-inverse\">备注</dt>\n" +
                    "            <dd class=\"col-sm-4\">\n" +
                    "              <p class=\"mg-b-10\">13143134312</p>\n" +
                    "            </dd>\n" +
                    "\n" +
                    "            <dt class=\"col-sm-2 tx-inverse\">公司名称</dt>\n" +
                    "            <dd class=\"col-sm-10\">\n" +
                    "              <p class=\"mg-b-10\">---------</p>\n" +
                    "            </dd>\n" +
                    "\n" +
                    "            <dt class=\"col-sm-2 tx-inverse\">创建人</dt>\n" +
                    "            <dd class=\"col-sm-4\">\n" +
                    "              <p class=\"mg-b-10\">13143134312</p>\n" +
                    "            </dd>\n" +
                    "\n" +
                    "            <dt class=\"col-sm-2 tx-inverse\">创建时间</dt>\n" +
                    "            <dd class=\"col-sm-4\">\n" +
                    "              <p class=\"mg-b-10\">2018-02-25</p>\n" +
                    "            </dd>\n" +
                    "            \n" +
                    "          </dl>\n" +
                    "        </div></div>";

                content += "<div class=\"modal-footer\">\n" +
                    "<button type=\"button\" class=\"btn btn-info pd-x-20\">编  辑</button>\n" +
                    "<button type=\"button\" class=\"btn btn-secondary pd-x-20\" data-dismiss='modal'>关  闭</button>\n" +
                    "</div>";

                $("#modalContent").html(content);
            }

        })
    });

    $("#customerLevelSel").change(function () {
        var levelId = $(this).children('option:selected').val();
        var nameOrContact = $('input[name="nameOrContact"]').val();
        var companyName = $('input[name="companyName"]').val();
        if (nameOrContact != null && nameOrContact.trim().length > 0) {
            location.href = "/crm/customer/list?nameOrContact=" + nameOrContact + "&levelId=" + levelId;
            return;
        } else if (nameOrContact.trim().length == 0 && companyName != null && companyName.trim().length > 0) {
            location.href = "/crm/customer/list?companyName=" + companyName + "&levelId=" + levelId;
            return;
        } else {
            location.href = "/crm/customer/list?levelId=" + levelId;
            return;
        }
    });

    $("#bt_search_nameOrContact").click(function () {
        var nameOrContact = $('input[name="nameOrContact"]').val();
        if(nameOrContact.trim().length > 0){
            $(location).attr('href', '/crm/customer/list?nameOrContact=' + nameOrContact);
            return;
        }
    });

    $("#bt_search_companyName").click(function () {
        var companyName = $('input[name="companyName"]').val();
        if (companyName.trim().length > 0) {
            $(location).attr('href', '/crm/customer/list?companyName=' + companyName);
            return;
        }
    });
});